package com.ff.xyh.consumer.dictionary.controller;


import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.dictionary.CheckStatus;
import com.ff.xyh.consumer.dictionary.service.CheckStatusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 审核状态表 前端控制器
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Api(tags = "审核状态类型")
@RestController
@RequestMapping("/dictionary")
public class CheckStatusController {

    @Autowired
    private CheckStatusService checkStatusService;

    @ApiOperation(value = "获取所有审核状态信息")
    @GetMapping("get-check-status-all")
    public ResultObject getAllCheckStatus() {
        List<CheckStatus> list = checkStatusService.list(null);
        Map map = new HashMap();
        map.put("items", list);
        return ResultObject.ok().data(map);
    }

    @ApiOperation(value = "根据id获取审核状态信息")
    @GetMapping("get-check-status-id")
    public ResultObject getCheckStatusById(
            @RequestParam(name = "id", required = false) Integer id) {
        CheckStatus checkStatus = checkStatusService.getById(id);
        if (checkStatus == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            Map map = new HashMap();
            map.put("data", checkStatus);
            return ResultObject.ok().data(map);
        }
    }

    @ApiOperation(value = "根据id删除审核状态信息")
    @GetMapping("delete-check-status-id")
    public ResultObject deleteCheckStatusById(
            @RequestParam(name = "id" , required = true) Integer id) {
        CheckStatus checkStatus = checkStatusService.getById(id);
        if ( checkStatus == null)
            return ResultObject.error().message("对象不存在");
        else {
            if (checkStatusService.removeById(id)) {
                Map map = new HashMap();
                map.put("data", checkStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_DELETE);
            }
        }
    }

    @ApiOperation(value = "添加审核状态信息")
    @PostMapping("insert-check-status")
    public ResultObject insertCheckStatus(
            @RequestBody CheckStatus checkStatus) {
        if (checkStatusService.getById(checkStatus.getId()) != null) {
            return ResultObject.error().message(ResultMessage.OBJECT_ALREADY_EXISTS);
        } else {
            if (checkStatusService.save(checkStatus)) {
                Map map = new HashMap();
                map.put("data", checkStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT_BUT_FAILED_TO_INSERT);
            }
        }
    }

    @ApiOperation(value = "修改审核状态信息")
    @PostMapping("update-check-status")
    public ResultObject updateCheckStatus(
            @RequestBody CheckStatus checkStatus) {
        if (checkStatusService.getById(checkStatus.getId()) == null) {
            return ResultObject.error().message(ResultMessage.NO_SUCH_OBJECT);
        } else {
            if (checkStatusService.updateById(checkStatus)) {
                Map map = new HashMap();
                map.put("data", checkStatus);
                return ResultObject.ok().data(map);
            } else {
                return ResultObject.error().message(ResultMessage.OBJECT_EXISTS_BUT_FAILED_TO_UPDATE);
            }
        }
    }
}

